package com.ruizhong.centercontroll.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruizhong.centercontroll.dao.TFrameMapper;
import com.ruizhong.centercontroll.entity.TFrame;
import com.ruizhong.centercontroll.service.TFrameService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class TFrameServiceImpl extends ServiceImpl<TFrameMapper, TFrame> implements TFrameService {

    /**@Description: TODO methods
     * @param
     * @return
     * @version v1.0
     * @author fengweijie
     * @date 2024/12/6 15:27
     */
    @Override
    public synchronized void submitNewIframe(TFrame tFrame) {
        List<TFrame> frames = lambdaQuery()
                .eq(TFrame::getIframeName, tFrame.getIframeName())
                .or()
                .eq(TFrame::getIframeNameInTraditional,tFrame.getIframeNameInTraditional())
                .or()
                .eq(TFrame::getIframeNameInEnglish,tFrame.getIframeNameInEnglish())
                .list();
        if (!CollectionUtils.isEmpty(frames)){
            throw new RuntimeException("页面名称重复");
        }
        save(tFrame);
    }

    @Override
    public List<TFrame> getAllIframe(Integer type,Integer monMenu) {
        List<TFrame> list = lambdaQuery()
                .eq(monMenu!=null && monMenu != -2,TFrame::getMonMenu,monMenu)
                .list();

        if (type == 1) {
            TFrame multimedia = new TFrame();
            multimedia.setIframeName("多媒体");
            multimedia.setIframeUrl(null);
            multimedia.setIframeNameInTraditional("多媒體");
            multimedia.setIframeNameInEnglish("Multimedia");
            multimedia.setId(-1);
            multimedia.setIframeUrlTraditional(null);
            multimedia.setIframeUrlEnglish(null);
            multimedia.setNeedPassword("2");
            multimedia.setPassword(null);
            list.add(0,multimedia);
        }
        return list;
    }

    @Override
    public TFrame getIframeDetailById(Integer id) {
        TFrame tFrame = getById(id);
        return tFrame;
    }

    @Override
    public synchronized void updateIframeInfo(TFrame tFrame) {
        if (tFrame.getIsAudioControl()==1){
            tFrame.setIframeUrl(null);
            tFrame.setIframeUrlTraditional(null);
            tFrame.setIframeUrlEnglish(null);
        }
        Integer monMenuId = tFrame.getMonMenu();
        if (monMenuId>=0){
            TFrame monItem = getById(monMenuId);
            if (monItem.getIsMenuItem().equals("2")){
                throw new RuntimeException("母菜单项不可为网页");
            }
        }
        TFrame frame = lambdaQuery().eq(TFrame::getIframeName,tFrame.getIframeName()).ne(TFrame::getId,tFrame.getId()).one();
        if (frame == null){
            lambdaUpdate()
                    .set(TFrame::getIframeName,tFrame.getIframeName())
                    .set(TFrame::getIframeNameInTraditional,tFrame.getIframeNameInTraditional())
                    .set(TFrame::getIframeNameInEnglish,tFrame.getIframeNameInEnglish())
                    .set(TFrame::getIframeUrl,tFrame.getIframeUrl())
                    .set(TFrame::getIframeUrlTraditional,tFrame.getIframeUrlTraditional())
                    .set(TFrame::getIframeUrlEnglish,tFrame.getIframeUrlEnglish())
                    .set(TFrame::getNeedPassword,tFrame.getNeedPassword())
                    .set(TFrame::getPassword,tFrame.getPassword())
                    .set(TFrame::getIsMenuItem,tFrame.getIsMenuItem())
                    .set(TFrame::getMonMenu,tFrame.getMonMenu())
                    .set(TFrame::getAudioServerIp,tFrame.getAudioServerIp())
                    .set(TFrame::getAudioServerPort,tFrame.getAudioServerPort())
                    .set(TFrame::getCommandToGetCurrentVolume,tFrame.getCommandToGetCurrentVolume())
                    .set(TFrame::getCommandToChangeCurrentVolume,tFrame.getCommandToChangeCurrentVolume())
                    .set(TFrame::getCommandToMuteCurrentVolume,tFrame.getCommandToMuteCurrentVolume())
                    .set(TFrame::getIsAudioControl,tFrame.getIsAudioControl())
                    .eq(TFrame::getId,tFrame.getId())
                    .update();
        }else{
            throw new RuntimeException("序列号已经重复");
        }
    }

    @Override
    public void deleteFrameById(Integer id) {
        removeById(id);
    }

    @Override
    public List<TFrame> getAllIframe() {
        List<TFrame> list = list();
        return list;
    }

    @Override
    public String checkMonMenuIsAim(Integer menuId, String aimMon) {
        TFrame tFrame = lambdaQuery()
                .eq(TFrame::getId, menuId)
                .one();
        if (tFrame == null){
            return "0";
        }
        return tFrame.getIframeName().equals(aimMon)?"1":"0";
    }
}
